#include "posviewwidget.h"
#include "ui_posviewwidget.h"
#include <QFileDialog>

PosViewWidget::PosViewWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PosViewWidget),
    showNumEnable(false)
{
    ui->setupUi(this);
    user_init();
}

PosViewWidget::~PosViewWidget()
{
    delete posView;
    delete ui;
}

//更新显示文件框列表
void PosViewWidget::updateFileList(bool yes)
{
    if( yes ){
        if( posView!=nullptr ){
            QStringList fileList = posView->getPos().keys();
            ui->fileNameListComboBox->clear();
            ui->fileNameListComboBox->addItems(fileList);
            ui->fileNameListComboBox->setCurrentText(posView->getCurrentShowFileName());
        }
    }
}

void PosViewWidget::user_init()
{
    scene = new QGraphicsScene();
    posView = new PosView(scene);

    connect(posView, &PosView::fileListChange, this, &PosViewWidget::updateFileList);
    connect(posView, &PosView::sendMsg, this, &PosViewWidget::updateMsgLable);

    ui->posViewVerticalLayout->addWidget(posView);
    posView->show();
}

//文件选择框改变
void PosViewWidget::on_fileNameListComboBox_currentIndexChanged(const QString &arg1)
{
    if( arg1.isNull() || arg1.isEmpty() ){
       if(posView!=nullptr) posView->cleanShow();
       return;
    }
    if( posView!=nullptr ){
        posView->updateShow(ui->fileNameListComboBox->currentText());
    }
}


void PosViewWidget::on_comboBox_currentIndexChanged(int index)
{
    PosView::SHOW_TYPE type = PosView::SHOW_TYPE::SHOW_TYPE_ALL;

    if( 0==index ){
        type = PosView::SHOW_TYPE::SHOW_TYPE_ALL;
    }else if( 1==index ){
        type = PosView::SHOW_TYPE::SHOW_TYPE_FIX;
    }else if( 2==index ){
        type = PosView::SHOW_TYPE::SHOW_TYPE_FLOAT;
    }else if( 3==index ){
        type = PosView::SHOW_TYPE::SHOW_TYPE_SIGNLE;
    }

    if( posView!=nullptr ){
        posView->showType(type);
    }
}

//删除文件对应的显示pos
void PosViewWidget::on_deleteCurrentToolButton_clicked()
{
    QString s = ui->fileNameListComboBox->currentText();
    if( posView!=nullptr ){
        posView->deletePointByFile(s);
        ui->fileNameListComboBox->removeItem(ui->fileNameListComboBox->currentIndex());
    }
}

//删除所有的pos
void PosViewWidget::on_deleteAllToolButton_clicked()
{
    if( posView!=nullptr ){
        posView->deletePoints();
    }
}

//显示数字
void PosViewWidget::on_numToolButton_clicked()
{
    showNumEnable = !showNumEnable;
    posView->showNumber(showNumEnable);
}

void PosViewWidget::updateMsgLable(const QString &msg)
{
    if( ""==msg ) return;
    ui->msgLabel->setText(msg);
}

void PosViewWidget::on_openFileToolButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open GNSS File"), "./", tr("Txt Files (*.pos *.obs *.txt)"));

    if( fileName.isNull() || fileName.isEmpty() ) return;

    posView->readPosFromFile(fileName);
}
